
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include <json-glib/json-gobject.h>

#include "chatapp.h"
#include "msgwin.h"
#include "account.h"

struct _MsgWindow
{
  GtkDialog parent;
};

typedef struct _MsgWindowPrivate MsgWindowPrivate;

struct _MsgWindowPrivate
{
	GSettings *settings;
	GtkWidget *history;
	GtkWidget *send_buffer;
	GtkWidget *send_button;
	GtkWidget *back;
	GtkWidget *notice;
};

G_DEFINE_TYPE_WITH_PRIVATE(MsgWindow, msg_window, GTK_TYPE_DIALOG)
extern void conn_send(gpointer ptr, const void *data, gsize size);
void back_cb (GtkButton *button, gpointer user_data)
{
}
void *history_add(gpointer data, const gchar *name, const gchar *msg, gsize len,
		gboolean dir)
{
	gchar *show = NULL;
	GtkWidget *scrolled_box = NULL, *hbox = NULL, *label = NULL;
	MsgWindow *win;
	MsgWindowPrivate *priv;

	win = data;
	priv = msg_window_get_instance_private (win);
	scrolled_box = priv->history;

	g_print("msg show:%ld\n", len);
	hbox = GTK_WIDGET(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2));
	gtk_flow_box_insert(GTK_FLOW_BOX(scrolled_box), GTK_WIDGET(hbox), -1);
	//gtk_container_add(GTK_CONTAINER(scrolled_box), GTK_WIDGET(hbox));
/*
	sys_time_uint64_to_str(time_uint64, time_str, sizeof(time_str));
	label = GTK_WIDGET(gtk_label_new(time_str));
	gtk_label_set_width_chars(GTK_LABEL(label), 8);
	gtk_container_add(GTK_CONTAINER(record_box), GTK_WIDGET(label));
*/
	label = GTK_WIDGET(gtk_label_new(name));
	gtk_label_set_width_chars(GTK_LABEL(label), 8);
	gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(label));

	label = GTK_WIDGET(gtk_label_new(msg));
	gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(label));

	gtk_widget_show_all(hbox);
	goto err;
err:
	if (show)
		g_free(show);
	return NULL;
}
void send_button_cb (GtkButton *button, gpointer user_data)
{
	const char *id = NULL, *peer_id = NULL, *msg = NULL;
  char *data = NULL;
	GtkTextBuffer *buffer;
	GtkTextIter start, end;
	MsgWindow *win;
	MsgWindowPrivate *priv;

	win = user_data;
	priv = msg_window_get_instance_private (win);

	id = g_settings_get_string(priv->settings, "id");
	peer_id = g_object_get_data(G_OBJECT(win), "peer_id");
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(priv->send_buffer));
	gtk_text_buffer_get_bounds(buffer, &start, &end);
	msg = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
	data = account_client_send_msg(id, msg, peer_id);
	conn_send(win, data, strlen(data));
	history_add(win, id, msg, strlen(msg), true);
	goto err;
err:
	if (data)
		g_free(data);
}
static void
msg_window_init (MsgWindow *win)
{
	const char *id = NULL;
	MsgWindowPrivate *priv;

	priv = msg_window_get_instance_private (win);
	gtk_widget_init_template (GTK_WIDGET (win));
	priv->settings = g_settings_new ("org.gtk.app");

	id = g_object_get_data(G_OBJECT(win), "peer_id");
	gtk_label_set_text(GTK_LABEL(priv->notice), id);
	g_signal_connect (priv->send_button, "clicked",
			G_CALLBACK (send_button_cb), win);
	g_signal_connect (priv->back, "clicked",
		    G_CALLBACK (back_cb), win);
}

static void
msg_window_dispose (GObject *object)
{
  MsgWindowPrivate *priv;

  priv = msg_window_get_instance_private (MSG_WINDOW (object));
  g_clear_object (&priv->settings);

  G_OBJECT_CLASS (msg_window_parent_class)->dispose (object);
}

static void
msg_window_class_init (MsgWindowClass *class)
{
	G_OBJECT_CLASS (class)->dispose = msg_window_dispose;

	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
					       "/org/gtk/app/msg.ui");
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			MsgWindow, history);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			MsgWindow, send_buffer);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			MsgWindow, send_button);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			MsgWindow, back);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			MsgWindow, notice);
}

MsgWindow *
msg_window_new (MsgWindow *win)
{
  return g_object_new (MSG_WINDOW_TYPE, "transient-for", win, "use-header-bar", TRUE, NULL);
}
